/**
 * @module InsertImage
 */
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import {
	viewToModelAttribute,
	modelToViewAttribute
} from '../../utils/converters';


export default class attributeExtendFsPath extends Plugin {
	static get pluginName() {
		return 'attributeExtendFsPath';
	}

	/**
	 * @inheritDoc
	 */

	/*static get requires() {
	 return [];
	 }*/
	init() {
		const editor = this.editor;
		const schema = editor.model.schema;
		const conversion = editor.conversion;


		/*for (let schemaName in schema.getDefinitions()) {
			let schemaItem = schema.getDefinition(schemaName);
			let allowed = schemaItem.isBlock || (schemaItem.isInline || false);


			if (allowed) {
				console.log(schemaName,schemaItem)
				schema.extend(schemaName, {allowAttributes: ['data-path']});

				conversion.for('downcast').add(modelToViewAttribute('data-path',schemaName))
			}

		}*/
		try{
			schema.extend('media', {allowAttributes: ['data-path']});
			conversion.for('downcast').add(modelToViewAttribute('data-path', 'media'));
		}catch (e) {

		}


		try{
			schema.extend('$text', {allowAttributes: ['data-path']});
			conversion.for('downcast').attributeToElement({
				model: {
					key: "data-path",
					name: "$text"
				},
				view: (attributeValue, writer) => {
					return writer.createAttributeElement('a', {"data-path": attributeValue}, {priority: 5});
				},
				converterPriority: 'low'
			});

			/*conversion.for( 'editingDowncast' ).attributeToElement( {
				model: {
					key:"data-path",
					name:"$text"
				},
				view: ( attributeValue, writer ) => {
					return writer.createAttributeElement( 'a', { "data-path": null }, { priority: 5 } );
				},
			} );*/
		}catch (e) {

		}


		['img', 'audio', 'video', 'a'].forEach(tag => {
			try{
				conversion.for('upcast').attributeToAttribute({
					model: 'data-path',
					view: {
						name: tag,
						key: 'data-path'
					}
				});
			}catch (e) {

			}

		});


	}
}
